package com.github.aloxc.plugin.restplus.popup;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.intellij.notification.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.ApplicationManagerEx;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.ui.JBColor;
import com.intellij.ui.awt.RelativePoint;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.management.relation.Relation;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import java.awt.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * 小提示
 *
 * @author liyh
 */
public class PopupBalloon {

    public static JBColor NORMAL = new JBColor(new Color(186, 238, 186), new Color(73, 117, 73));
    private static JBColor ERROR = new JBColor(Consts.MAIN_COLOR, Consts.MAIN_COLOR);

    public static void showPopupBalloon(final Editor editor, final String result,boolean error) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                JBPopupFactory factory = JBPopupFactory.getInstance();
                factory.createHtmlTextBalloonBuilder(result, null, error ? ERROR : NORMAL,
                        e->{
                            if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
                                return;
                            }
                            URL url = e.getURL();
                            try {
                                Desktop.getDesktop().browse(url.toURI());
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            } catch (URISyntaxException ex) {
                                ex.printStackTrace();
                            }
                        })
                        .setFadeoutTime(5000)
                        .createBalloon()
                        .show(factory.guessBestPopupLocation(editor), Balloon.Position.above);
            }
        });
    }
    public static void showPopupBalloon(final JComponent component, final String text) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                JBPopupFactory factory = JBPopupFactory.getInstance();
                factory.createHtmlTextBalloonBuilder(text, null, NORMAL,null)
                        .setFadeoutTime(5000)
                        .createBalloon()
                        .show(factory.guessBestPopupLocation(component), Balloon.Position.above);
            }
        });
    }
    public static void showPopupBalloon(final JComponent component, final String result,boolean error) {

        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                JBPopupFactory factory = JBPopupFactory.getInstance();
                Balloon b;
                factory.createHtmlTextBalloonBuilder(result, null, error ? ERROR : NORMAL, e->{
                    if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
                        return;
                    }
                    URL url = e.getURL();
                    try {
                        Desktop.getDesktop().browse(url.toURI());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (URISyntaxException ex) {
                        ex.printStackTrace();
                    }
                })
                        .setTitle("RestPlus")
                        .setFadeoutTime(5000)
                        .createBalloon()
                        .show(factory.guessBestPopupLocation(component), Balloon.Position.below);
            }
        });
    }
    public static void showPopupBalloon(RelativePoint point, final String result, boolean error) {

        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                JBPopupFactory factory = JBPopupFactory.getInstance();
                Balloon b;
                factory.createHtmlTextBalloonBuilder(result, null, error ? ERROR : NORMAL, e->{
                    if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
                        return;
                    }
                    URL url = e.getURL();
                    try {
                        Desktop.getDesktop().browse(url.toURI());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (URISyntaxException ex) {
                        ex.printStackTrace();
                    }
                })
                        .setTitle("RestPlus")
                        .setFadeoutTime(5000)
                        .createBalloon()
                        .show(point, Balloon.Position.below);
            }
        });
    }

    private static final NotificationGroup NOTIFICATION_GROUP = NotificationGroup.toolWindowGroup("HTTP Client", ToolWindowId.RUN, true);


//    public static void showWarningBalloon(@Nullable Project project, @NotNull String title, @NotNull String text) {
//        NOTIFICATION_GROUP.createNotification(title, text, NotificationType.WARNING, null).notify(project);
//    }


    public static void showErrorBalloon(@Nullable Project project, @NotNull String title, @NotNull String text) {
        NOTIFICATION_GROUP.createNotification(title, text, NotificationType.ERROR, null).notify(project);
    }


    public static void showNotification(Project project, String info, boolean restart) {
        //
        String stickyBalloonDisplayId = "RestPlus IDEA Notification";
        String title = "RestPlus Intellij IDEA Plugin";

        NotificationGroup notificationGroup = new NotificationGroup(stickyBalloonDisplayId, NotificationDisplayType.STICKY_BALLOON, true);
        notificationGroup.createNotification("", info,
                NotificationType.INFORMATION, new NotificationListener() {
                    @Override
                    public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent hyperlinkEvent) {
                        notification.expire();
                        if(restart) {
                            ApplicationManagerEx.getApplicationEx().restart(restart);
                        }else{
                            if (hyperlinkEvent.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
                                return;
                            }
                            URL url = hyperlinkEvent.getURL();
                            try {
                                Desktop.getDesktop().browse(url.toURI());
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            } catch (URISyntaxException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }).notify(project);
    }

}
